package app.modules.cloudpivot.function;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.authine.cloudpivot.engine.api.model.runtime.SelectionValue;
import com.authine.cloudpivot.engine.workflow.spi.context.WorkflowActivityContext;
import com.authine.cloudpivot.engine.workflow.spi.function.ParticipantFunction;

/**
 * 自定义参与者函数
 *
 * @author LiuGangQiang Create in 2023/03/22
 */
public class CustomParticipantFunction extends ParticipantFunction {
    private static final long serialVersionUID = -173931992492578947L;
    private static final Logger log = LoggerFactory.getLogger(CustomParticipantFunction.class);

    /**
     * 参与者函数
     *
     * @author LiuGangQiang Create in 2023/03/22
     * @param users           人员单选或多选
     * @param activityContext 上下文
     * @return {@link List} 用户ID集合
     */
    public List<String> findParticipants(List<SelectionValue> users, WorkflowActivityContext activityContext) {
        log.info("===>>执行自定义参与者函数 参数类型:{} 参数:{} ", users.getClass().getCanonicalName(), users);
        if (users != null && users.size() > 0) {
            return users.stream().map(u -> u.getId()).distinct().collect(Collectors.toList());
        } else {
            return new ArrayList<String>();
        }
    }

    @Override
    public String getName() {
        return "CustomParticipantDemo";
    }

    @Override
    public String getFullName() {
        return "CustomParticipantDemo()";
    }

    @Override
    public String getDisplayName() {
        return "二开的参与者函数";
    }

    @Override
    public String getDescription() {
        return "CustomParticipantDemo(x),自定义参与者函数,目前未实现什么逻辑,只是简单的将人员单选或多选内的用户ID获取出来。大家可以自行实现对应的选人逻辑，请注意的是此函数只支持人员单选多选控件作为参数，其他参数会异常";
    }
}